import * as types from '@/store/mutations-types'
// import store from '@/store'
/**
 * vuex actions操作方法封装
 * @param commit veux的commit、用于提交数据给mutations
 * @param loadingName       成功后mutations处理loading的名称
 * @param api               http api接口名称
 * @param payload           http api提交内容
 * @param success           成功的回调
 * @returns {Promise<unknown>}
 */
export const apiOperation = ({ commit, loadingName, api, payload }, success) => {
  return new Promise(resolve => {
    // resolve, reject
    commit({ type: types.LOADING, name: loadingName || api.name, value: true })
    api(payload)
      .then(res => {
        // 统一拦截接口返回的用户权限privileges
        success && success(res)
        resolve(res)
      })
      .finally(() => {
        commit({ type: types.LOADING, name: loadingName || api.name, value: false })
      })
      .catch(() => {})
  })
}
